package com.peanut.user.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.peanut.common.pojo.User;
import com.peanut.common.utils.CookieUtils;
import com.peanut.common.vo.SysResult;
import com.peanut.user.service.UserService;
import com.peanut.user.utils.VerifyCode;

@RestController
@RequestMapping("user/manage")
public class UserController {
	@Autowired
	private UserService userService;
	//用户名的存在校验
	@RequestMapping("checkUserName")
	public SysResult checkUserName(String userName){
		int exist=
			userService.checkUserName(userName);
		//判断返回的sysResult值
		if(exist>=1){//不可用 status=201
			return SysResult.build(201, "已经存在", null);
		}else{//可用
			return SysResult.ok();
		}
	}
	//注册表单提交
	@RequestMapping("save")
	public SysResult saveUser(User user,String vcode,HttpSession req){
		String code = (String) req.getAttribute("code");
		try{
			if(!code.equalsIgnoreCase(vcode)){
				return SysResult.build(201, "验证码不正确！", null);
			}else{
			int exist=
					userService.checkUserName(user.getUserName());
				//判断返回的sysResult值
				if(exist>=1){//不可用 status=201
					return SysResult.build(201, "用户名已经存在", null);
				}else{//可用
					userService.saveUser(user);//业务层封装数据完整
					return SysResult.ok();
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			return SysResult.build(201, "注册失败", null);
		}
	}
	//登录校验
	@RequestMapping("login")
	public SysResult doLogin(User user,HttpServletRequest
			req,HttpServletResponse res){
		System.out.println("13213213213");
		//在业务层中实现登录逻辑,存储redis过程
		//返回给控制层一个生成的redis存储ticket值
		//登录成功,ticket是非空,登录失败返回空null/""
		String ticket=userService.doLogin(user);
		//System.out.println(ticket);
		//System.out.println(user);
		if(StringUtils.isNotEmpty(ticket)){
			//ticket非空,说明登录成功
			//cookie携带这个ticket返回浏览Cookie的工具类
			CookieUtils.setCookie(req, res, "EM_TICKET", ticket);
			//返回sysresult status=200
			System.out.println("登陆成功");
			return SysResult.ok();
		}else{
			//ticket为空登录失败
			return SysResult.build(201, "登录失败", null);
		}
	}
	//通过js发送的参数ticket获取userJson封装返回给ajax使用
	@RequestMapping("/query/{ticket}")
	public SysResult checkLoginUserData(@PathVariable
			String ticket){
		String userJson=
			userService.checkLoginUserData(ticket);
		if(StringUtils.isNotEmpty(userJson)){
			//说明userJson非空 说明登录状态正在生效
			return SysResult.build
					(200, "登录正常", userJson);
		}else{//登录失效超时
			return SysResult.build
					(201, "登录失效", null);
		}
	}
	/**
	 * 获取验证码
	 */
	@RequestMapping("/valiImg.action")
	public void valiImg(HttpServletResponse response,HttpSession session) throws Exception {
		//1.控制浏览器不使用缓存
        response.setDateHeader("Expires",-1);
        response.setHeader("Cache-Control","no-cache");
        response.setHeader("pragma","no-cache");
        //2.生成验证码图片。
        VerifyCode vc = new VerifyCode();
        vc.drawImage(response.getOutputStream());
        //3.获取验证码图片字符
        String code = vc.getCode();
        //4.将生成的验证码加入session域中
        session.setAttribute("code",code);
        System.out.println(code);
	} 
	@RequestMapping("/loginout")
	public SysResult loginout(HttpServletRequest request,HttpServletResponse response) {
		CookieUtils.deleteCookie(request, response, "EM_TICKET");
		System.out.println("删除成功");
		return SysResult.ok();
		
	}

}
